package com.it.elmboot.controller;

import com.it.elmboot.pojo.User;
import com.it.elmboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/saveUser")
    @ResponseBody
    public ResponseEntity<Object> saveUser(@RequestBody User user) {
        boolean isSuccess = userService.saveUser(user);
        if (isSuccess) { 
            return new ResponseEntity<>("注册成功！", HttpStatus.OK);
        } else {
            return new ResponseEntity<>("用户已存在，可直接登录！", HttpStatus.BAD_REQUEST);
        }
    }

    @PostMapping("/getUserByIdByPass")
    @ResponseBody
    public ResponseEntity<Object> getUserByIdByPass(@RequestBody User user) {

         User userByIdByPass = userService.getUserByIdByPass(user);

        if (userByIdByPass != null) {
            return new ResponseEntity<>(userByIdByPass, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

}
